package com.jky.trial.service.impl;

import com.jky.trial.common.ListResult;
import com.jky.trial.common.Pagination;
import com.jky.trial.constant.DataStatus;
import com.jky.trial.entity.Device;
import com.jky.trial.entity.DeviceExample;
import com.jky.trial.mapper.DeviceMapper;
import com.jky.trial.service.DeviceService;
import com.jky.trial.vo.DeviceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {
    private final DeviceMapper recordMapper;
    DeviceExample recordExample = new DeviceExample();
    DeviceExample.Criteria criteria = null;

    @Autowired
    DeviceServiceImpl(DeviceMapper recordMapper) {
        this.recordMapper = recordMapper;
    }

    @Override
    public int create(Device record) {
        record.setCreateAt(new Date());
        record.setStatus(DataStatus.DEVICE_STATUS_NORMAL.getVal());
        record.setInStoreStatus(true);
        return recordMapper.insertSelective(record);
    }

    @Override
    public int update(Device record) {
        DeviceExample recordExample = new DeviceExample();
        DeviceExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        return recordMapper.updateByExampleSelective(record, recordExample);
    }

    @Override
    public int del(int id) {
        return recordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Device detail(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<DeviceVo> list(DeviceVo record) {
        return recordMapper.pageList(record);
    }

    @Override
    public List<DeviceVo> listWithCreatorName(DeviceVo record) {
        return recordMapper.pageList(record);
    }

    @Override
    public ListResult pageWithCreatorName(DeviceVo record) {
        List<DeviceVo> list = listWithCreatorName(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult page(DeviceVo record) {
        List<DeviceVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public long count(Device record) {
        criteria = recordExample.createCriteria();
        if (record.getSearch() != null) { //按名称搜索
            criteria.andNameLike(record.getSearch() + '%');
        }
        if (record.getStatus() != null) { //按状态筛选
            criteria.andStatusEqualTo(record.getStatus());
        }
        return recordMapper.countByExample(recordExample);
    }
}
